/*
 * Mask.java
 *
 * Created on June 1, 2007, 2:09 AM
 *
 *  Copyright (C) 2010 Tal Eisenberg
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package base;

import tal.drivechain.variable.Variable;
import tal.drivechain.matchers.VarMatcher;
import tal.drivechain.plugin.Module;
import java.util.List;

import tal.drivechain.plugin.Channel;
import tal.drivechain.variable.SetProperty;

/**
 *
 * @author Main
 */
public class Send extends Module {
    List<Object[]> messages;
        
    @Override
    public void setupProperties() {
        setTriggerMessage(props.getProperty("triggerMessage"), null);
    }
    
    public void setTriggerMessage(Object value, SetProperty.Flag flag) {
        if (flag==SetProperty.Flag.ADD) messages.addAll((List)value);
        else messages = (List)value;
    } public Object getTriggerMessage() { return messages; }

    @Override
    protected void msgReceived(Variable in, long timestamp, Channel channel) {
        if (messages==null) return;
        for (Object[] message : messages) {
            VarMatcher trigger = (VarMatcher)message[0];
            if (trigger.matches(in, channel, this)) {
                try {
                    Variable out = (Variable)message[1];
                    sendToOutputs(out, timestamp);
                } catch (IllegalArgumentException ie) {
                    log(ie.getMessage(), MSG.WARNING);
                }
            }
        }
    }
}
